package com.wisdytech.linkdcs.tool.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.PojoConvertUtil;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.dao.ToolCheckItemBakDao;
import com.wisdytech.linkdcs.tool.dao.ToolCheckItemDao;
import com.wisdytech.linkdcs.tool.model.ToolCheckItem;
import com.wisdytech.linkdcs.tool.model.ToolCheckItemBak;
import com.wisdytech.linkdcs.tool.service.IToolCheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-12
 */
@Service
@Transactional
public class ToolCheckItemServiceImpl extends BaseService<ToolCheckItemDao, ToolCheckItem> implements IToolCheckItemService {

    @Resource
    private ToolCheckItemDao toolCheckItemDao;

    @Resource
    private ToolCheckItemBakDao toolCheckItemBakDao;

    @Override
    public PageInfo<ToolCheckItem> getToolCheckItemByFilter(String toolCode, Filter filter) {
        if (StringUtils.isBlank(toolCode)) {
            return new PageInfo<>(new ArrayList<>());
        }
        EntityWrapper<ToolCheckItem> ew = new EntityWrapper<>();
        ew.eq("del_flag", CommonConstants.DEL_FLAG)
                .eq("tool_code", toolCode);
        List<ToolCheckItem> list = this.beforeList(ToolCheckItemDao.class, filter).selectList(ew);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean saveOrUpdateCheckItem(ToolCheckItem toolCheckItem, SysUser operator) {
        Integer operateCount = 0;
        if (operator == null)
            throw new ServiceException("操作人不能为空");
        if (toolCheckItem == null || StringUtils.isBlank(toolCheckItem.getCheckProject()) || StringUtils.isBlank(toolCheckItem.getToolCode()))
            throw new ServiceException("参数错误");

        EntityWrapper<ToolCheckItem> ew = new EntityWrapper<>();
        ew.eq("tool_code", toolCheckItem.getToolCode())
                .eq("check_project", toolCheckItem.getCheckProject())
                .eq("del_flag", CommonConstants.DEL_FLAG);
        if (StringUtils.isNotBlank(toolCheckItem.getPkId())) {
            ew.ne("pk_id", toolCheckItem.getPkId());
        }
        Integer count = toolCheckItemDao.selectCount(ew);
        if (count > 0) {
            throw new ServiceException("校验项目重复");
        }

        String pkId = BaseUtil.getUUID();

        String snapshotId = BaseUtil.getUUID();
        //修改
        if (StringUtils.isNotBlank(toolCheckItem.getPkId())) {
            pkId = toolCheckItem.getPkId();
            toolCheckItem.setUpdateDate(new Date());
            toolCheckItem.setUpdateUser(operator.getUsername());
            toolCheckItem.setVersion(snapshotId);
            operateCount = toolCheckItemDao.updateById(toolCheckItem);
        } else {
            //新增
            toolCheckItem.setPkId(pkId);
            toolCheckItem.setCreateDate(new Date());
            toolCheckItem.setCreateUser(operator.getUsername());
            toolCheckItem.setDelFlag(CommonConstants.DEL_FLAG);
            toolCheckItem.setVersion(snapshotId);
            operateCount = toolCheckItemDao.insert(toolCheckItem);
        }

        // 查询快照
        ToolCheckItem snapshot = toolCheckItemDao.selectById(pkId);
        // 复制快照
        ToolCheckItemBak toolCheckItemBak = PojoConvertUtil.convertPojo(snapshot,ToolCheckItemBak.class);
        toolCheckItemBak.setPkId(snapshotId);
        toolCheckItemBak.setDelFlag(CommonConstants.DEL_FLAG);
        // 保存快照
        toolCheckItemBakDao.insert(toolCheckItemBak);


        return operateCount > 0;
    }

    @Override
    public Boolean deleteToolCheckItem(String pkIds, SysUser operator) {
        if (operator == null)
            throw new ServiceException("操作人不能为空");
        String[] deletePkIds = pkIds.split(",");
        if (deletePkIds.length == 0)
            return true;
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        Integer deleteCount = toolCheckItemDao.deleteBatchIds(pkIdCollection);
        return deleteCount > 0;
    }

    @Override
    public ToolCheckItem getToolCheckItemById(String pkId) {
        if (StringUtils.isBlank(pkId)) {
            return null;
        }
        return toolCheckItemDao.selectById(pkId);
    }
}
